<?php

namespace Web\MainSiteBundle\Controller;

use BaseExt\Pagination;
use Model\CommentsBundle\Model\CommentsModel;
use Model\CommentsReplyBundle\Model\CommentsReplyModel;
use Model\CourseBundle\Entity\Course;
use Model\CourseBundle\Entity\CourseLessonArrange;
use Model\CourseBundle\Model\CourseLessonArrangeModel;
use Model\CourseBundle\Model\CourseLessonContentModel;
use Model\CourseBundle\Model\CourseModel;
use Model\CourseCategoryBundle\Model\CourseCategoryModel;
use Model\CourseCollectBundle\Model\CourseCollectModel;
use Model\CourseLikesBundle\Model\CourseLikesModel;
use Model\ExamBundle\Model\ExamModel;
use Model\NoteBundle\Model\NoteModel;
use Model\NoteReplyBundle\Model\NoteReplyModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("course")
 */
class CourseController extends WebBaseController
{
    /**
     * @route("/index/{uniqueId}",name="WebCourseIndex")
     */
    public function WebCourseIndex(Request $request , Course $course)
    {
        $data["course"] = $course;
        $courseId = $course->getId();
        $courseModel = new CourseModel($this->getDoctrine());
        $courseLikes = new CourseLikesModel($this->getDoctrine());
        $courseCollect = new CourseCollectModel($this->getDoctrine());
        $courseLessonArrangeModel = new CourseLessonArrangeModel($this->getDoctrine());
        $courseLessonContentModel = new CourseLessonContentModel($this->getDoctrine());

        $session = $request->getSession();
        $myCourseHit = $session->get("myCourseHit",array());
        if (!in_array($courseId,$myCourseHit)) {
            $courseModel->addHit($courseId);
            array_push($myCourseHit,$courseId);
            $session->set("myCourseHit",$myCourseHit);
        }

        $arrange = $courseLessonArrangeModel->getAllCourseLessonArrangeTree($courseId);
        $firstLesson = null;
        if (empty($arrange)) {
            $arrange = array(array(
                "id"=>0,
                "title"=>"全部课程",
                "level"=>0,
                "orders"=>0,
            ));
        }
        foreach ($arrange as $k => &$v) {
            $lessons = $courseLessonContentModel->getListForCourseArrange($v["id"],$courseId);
            if (!empty($lessons) && $firstLesson === null) {
                $firstLesson = $lessons[0];
            }
            $v["lessons"] = $lessons;
        }
        $data["courseLessonArrange"] = $arrange;
        $data["firstLesson"] = $firstLesson;

        if ($user = $this->chkIsUser()) {
            $userId = $user->getId();
        } else {
            $userId = 0 ;
        }

        $data["userLikes"] = $courseLikes->chkIfLikes($userId,$courseId);
        $data["courseLikes"] = $courseLikes->countCourseLikes($courseId);

        $data["userCollect"] = $courseCollect->chkIfCollect($userId,$courseId);
        $data["courseCollect"] = $courseCollect->countCourseCollect($courseId);


        return $this->render("WebMainSiteBundle:Web:course_index.html.twig",$data);
    }
    /**
     * @route("/learn/{uniqueId}",name="WebCourseLessons")
     */
    public function WebCourseLessons(Request $request , Course $course)
    {
        $user = $this->needToBeUser();
        if (!$lessonId = $this->getParamRequired("id")) {$this->showMessagePage("参数丢失");}
        if (!$pageComments = $this->getParamRequired("pc")) {$pageComments = 1;}
        if (!$pageNote = $this->getParamRequired("pn")) {$pageNote = 1;}
        $data["course"] = $course;

        $courseId = $course->getId();
        $courseLessonArrangeModel = new CourseLessonArrangeModel($this->getDoctrine());
        $courseLessonContentModel = new CourseLessonContentModel($this->getDoctrine());
        $arrange = $courseLessonArrangeModel->getAllCourseLessonArrangeTree($courseId);

        //获取课程内容，如果没有选择课程进入本页面则默认显示第一节课
        $firstLesson = null;
        if (empty($arrange)) {
            $arrange = array(array(
                "id"=>0,
                "title"=>"全部课程",
                "level"=>0,
                "orders"=>0,
            ));
        }
        foreach ($arrange as $k => &$v) {
            $lessons = $courseLessonContentModel->getListForCourseArrange($v["id"],$courseId);
            if (!empty($lessons) && $firstLesson === null) {
                $firstLesson = $lessons[0];
            }
            $v["lessons"] = $lessons;
        }
        $curLesson = $courseLessonContentModel->getOneArray($lessonId,$course->getId());
        if (empty($curLesson)) {
            $data["courseLesson"] = $firstLesson;
        }else{
            $data["courseLesson"] = $curLesson;
        }
        //获取课程目录
        $data["courseLessonArrange"] = $arrange;

        //获取评论
        $commentsModel = new CommentsModel($this->getDoctrine());
        $commentsReplyModel = new CommentsReplyModel($this->getDoctrine());
        $comments = $commentsModel->getListByLesson($data["courseLesson"]["id"],$pageComments*10-10);
        foreach ($comments as &$comment) {
            $comment["reply"] = $commentsReplyModel->getListByWhom($comment["id"]);
        }
        $baseUrl = $this->generateUrl("WebCourseLessons",array("uniqueId"=>$course->getUniqueId(),"id"=>$lessonId,"pc"=>1));
        $data["courseComments"] = $comments;
        $pageParam["per_page"] = "10";
        $pageParam["base_url"] = "";
        $pageParam["query_string_segment"] = "pc";
        $pageParam["first_url"] = $baseUrl."";
        $pageParam["total_rows"] = $commentsModel->countComments($data["courseLesson"]["id"]);
        $PageModel = new Pagination($pageParam);
        $page = $PageModel->create_links();
        $data["page"] = $page;

        //获取笔记
        $noteModel = new NoteModel($this->getDoctrine());
        $noteReplyModel = new NoteReplyModel($this->getDoctrine());
        $note = $noteModel->getListByLesson($data["courseLesson"]["id"],$pageNote*10-10);
        foreach ($note as &$comment) {
            $comment["reply"] = $noteReplyModel->getListByWhom($comment["id"]);
        }
        $baseUrl = $this->generateUrl("WebCourseLessons",array("uniqueId"=>$course->getUniqueId(),"id"=>$lessonId,"pn"=>1));
        $data["courseNote"] = $note;
        $pageParam["per_page"] = "10";
        $pageParam["base_url"] = "";
        $pageParam["query_string_segment"] = "pn";
        $pageParam["first_url"] = $baseUrl."";
        $pageParam["total_rows"] = $noteModel->countNote($data["courseLesson"]["id"]);
        $PageModel = new Pagination($pageParam);
        $page = $PageModel->create_links();
        $data["pageNote"] = $page;

        //点击开始学习自动加入收藏
        $CourseCollectModel = new CourseCollectModel($this->getDoctrine());
        $whom = $courseId;
        $userId = $user->getId();
        if (!$CourseCollectModel->chkIfCollect($userId,$whom)) {
            $params = array(
                "who"   => $userId,
                "whom"  => $whom,
            );
            $CourseCollectModel->commonAdd($params);
        }

        return $this->render("WebMainSiteBundle:Web:lessons.html.twig",$data);
    }


    /**
     * @route("/Exams/Select/{uniqueId}",name="WebCourseExamSelect")
     */
    public function WebCourseExamSelect(Request $request , Course $course)
    {
        $user = $this->needToBeUser();
        $courseId = $course->getId();

        $userId = $user->getId();
        $examModel = new ExamModel($this->getDoctrine());
        $examMixed = $examModel->getFromCourse4TestOrExam($courseId);
        $newData = array(
            "test"  => array(),
            "exam"  => array()
        );

        foreach ($examMixed as $item) {
            if ($item["type"] == 1) {
                $newData["exam"][] = $item;
            } else {
                $newData["test"][] = $item;
            }
        }

        $data = array(
            "exam" => $newData,
            "course" => $course,
            "examMixed"   => $examMixed,
        );
        return $this->render("WebMainSiteBundle:Course:course_exam_select.html.twig",$data);

    }

}
